package niuke;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author wangmaolin1
 * @date 2022/7/30 10:52 上午
 */
public class _93LRU {
    private MyLRU<Integer,Integer>lru;

    public _93LRU(int capacity) {
        // write code here
        this.lru=new MyLRU<>(capacity,0.75f);
    }

    public int get(int key) {
        // write code here
        if (lru.containsKey(key)){
            return (int) lru.get(key);
        }
        return -1;
    }

    public void set(int key, int value) {
        // write code here
        lru.put(key,value);
    }
    public static class MyLRU<K,V> extends LinkedHashMap{
        private int maxSize;

        public MyLRU(int initialCapacity, float loadFactor) {
            super(initialCapacity, loadFactor,true);
            this.maxSize = initialCapacity;
        }

        @Override
        protected boolean removeEldestEntry(Map.Entry eldest) {
            return size()>maxSize;
        }
    }
}
